<template>
  <div class="my-tag" @dblclick="tagEdit">
    <div class="text" v-if="isShow" ref="tagName">{{ value }}</div>
    <input
      v-else
      class="input"
      type="text"
      placeholder="输入标签"
      v-focus
      @blur="isShow = true"
      @keyup.esc="isShow = true"
      ref="tagInput"
      :value="value"
      @keyup.enter="edit"
    />
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      isShow: true,
    };
  },
  methods: {
    tagEdit() {
      this.isShow = false;
    },
    edit(e) {
      if (e.target.value.trim() === "") {
        return (this.isShow = true);
      }
      this.$emit("input", e.target.value);
      this.isShow = true;
    },
  },
};
</script>

<style lang="less">
.my-tag {
  cursor: pointer;

  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;

    &::placeholder {
      color: #666;
    }
  }
}
</style>
